package cn.tedu._05mvcboot03.controller;

import cn.tedu._05mvcboot03.common.respose.JsonResult;
import cn.tedu._05mvcboot03.common.respose.StatusCode;
import cn.tedu._05mvcboot03.mapper.MvcUserMapper;
import cn.tedu._05mvcboot03.pojo.dto.UserLoginDTO;
import cn.tedu._05mvcboot03.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
@Api(tags = "01.用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/users/")
public class MvcUserController {

    @Autowired
    private MvcUserMapper mvcUserMapper;

    @ApiOperation(value = "登录")
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session) {
        log.debug("userLoginDTO:" + userLoginDTO);
        UserVO userVO = mvcUserMapper.selectByUsername(userLoginDTO.getUsername());
        if (userVO==null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        if (userLoginDTO.getPassword().equals(userVO.getPassword())){
            session.setAttribute("user",userVO);
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);


    }
}
